{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 生成器是可以暂停的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GEN_CREATED\n",
      "GEN_SUSPENDED\n",
      "None\n",
      "GEN_CLOSED\n"
     ]
    }
   ],
   "source": [
    "import inspect\n",
    "\n",
    "def gen_func():\n",
    "    ## 1 返回值给调用方\n",
    "    ## 2 调用方通过send方式返回值给gen\n",
    "    value = yield 1\n",
    "    ## value 为调用方传入的值\n",
    "    print(value)\n",
    "    return \"bobby\"\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    gen = gen_func()\n",
    "    print(inspect.getgeneratorstate(gen))\n",
    "    next(gen)\n",
    "    print(inspect.getgeneratorstate(gen))\n",
    "    try:\n",
    "        next(gen)\n",
    "    except StopIteration:\n",
    "        pass\n",
    "    print(inspect.getgeneratorstate(gen))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
